package library;

import java.sql.SQLException;
import java.util.List;

import com.j256.ormlite.dao.Dao;

abstract public class UserManager {
	protected Dao<User, String> daoUser;
	protected User currentUser;
	
	public <cls>UserManager(Class<cls> cls) throws SQLException{
		daoUser = DaoUtil.getDao(cls);
	}
	
	public User getCurrentUser(){
		return currentUser; 
	}
	
	protected boolean create(User user) throws SQLException{
		return (daoUser.create(user) == 1);
	}
	
	public boolean delete(User user) throws SQLException {
		return (daoUser.delete(user) == 1);
	}
	
	protected User search(String userName) throws Exception{
		List<User> userList = null;
		userList = daoUser.queryForEq("name", userName);
		if(userList.size() <= 0)
			throw new Exception("User :" + userName + " not found");
		return userList.get(0);
	}
}
